<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class Labor extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'admin_id',
        'company_id',
        'age',
        'name',
        'mobile',
        'photo',
        'sex',
        'work',
        'address',
        'id_card',
        'home_address',
        'birth',
        'nationality',
        'modality',
        'emergency_contact',
        'emergency_mobile',
        'wx',
        'zfb',
        'bank',
        'bank_number',
        'line_number',
        'bank_deposit',
        'remarks',
        'third_edu',
        'paper_file',
        'id_card_personal',
        'id_card_emblem',
    ];

    const POSTONLY = [
        'admin_id',
        'company_id',
        'age',
        'name',
        'mobile',
        'photo',
        'sex',
        'work',
        'address',
        'id_card',
        'home_address',
        'birth',
        'nationality',
        'modality',
        'emergency_contact',
        'emergency_mobile',
        'wx',
        'zfb',
        'bank',
        'bank_number',
        'line_number',
        'bank_deposit',
        'remarks',
        'third_edu',
        'paper_file',
        'id_card_personal',
        'id_card_emblem',
    ];

    protected $hidden = [
        'password',
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function black()
    {
        return $this->hasOne(LaborBlack::class);
    }

    // 关联创建人
    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    public function resources()
    {
        return $this->hasMany(ResourceBind::class,'source_id','id')->where('model','Labor')->with('resource');
    }

    public function LaborAttendances()
    {
        return $this->hasMany(LaborAttendance::class,'labor_id','id')->where('work_hours','>',0);
    }
}
